package org.csu.ssm_backend.web;

import cn.hutool.crypto.Mode;
import jakarta.servlet.http.HttpSession;

import org.csu.ssm_backend.domain.Account;
import org.csu.ssm_backend.domain.Order;
import org.csu.ssm_backend.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpSession session;

    private Order order;

    /**
     * 跳转到订单管理页面
     * */
    @RequestMapping("/orders")
    public ModelAndView changeToOrders(HttpSession session){
        Account account = (Account) session.getAttribute("account");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("03");
        List<Order> orderList = orderService.getOrderList(account.getUserid());
        modelAndView.addObject("orderList",orderList);
        return modelAndView;
    }

    /**
     * 删除订单信息
     * */
    @DeleteMapping("/orders/delete")
    @ResponseBody
    public String DeleteOrder(
            @RequestParam("userid") String userid,
            @RequestParam("orderId")String orderId
    ){
//        ModelAndView modelAndView = new ModelAndView("redirect:/orders");
        System.out.println("hello");
        System.out.println(userid + "," + orderId);
        Account account = (Account) session.getAttribute("account");
        orderService.deleteOrder(account.getUserid(),orderId);
        return "success";
//        return modelAndView;
    }
}
